package java;

import java.util.LinkedList;

public class Question12 {
    public static void main(String[] args) {
        System.out.println(intToRoman(58));
    }
    public static String intToRoman(int num) {
        if (num < 1){
            return "";
        }
        int time = 0;
        char[][] signs = {{'I','V'},{'X','L'},{'C','D'},{'M',' '}};
        StringBuffer res = new StringBuffer();
        do {
            StringBuffer temp = new StringBuffer();
            int oneDigit = num % 10;
            if (oneDigit > 0 && oneDigit < 4){
                for (int i = 0;i < oneDigit; i ++){
                    temp.append(signs[time][0]);
                }
            }else if (oneDigit == 4){
                temp.append(signs[time][0]);
                temp.append(signs[time][1]);
            }else if (oneDigit > 4 && oneDigit < 9){
                temp.append(signs[time][1]);
                for (int i = 0;i < oneDigit - 5;i ++){
                    temp.append(signs[time][0]);
                }
            }else if (oneDigit == 9){
                temp.append(signs[time][0]);
                temp.append(signs[time + 1][0]);
            }
            temp = temp.reverse();
            res.append(temp.toString());
            time ++;
            num /= 10;
        }while (num != 0);
        return res.reverse().toString();
    }
}
